//
//  RLLineChartView.m
//  testLineChart
//
//  Created by LongJun on 13-12-21.
//  Copyright (c) 2013年 LongJun. All rights reserved.
//
#if !__has_feature(objc_arc)
#error "This source file must be compiled with ARC enabled!"
#endif

#import "ARLineChartView.h"
#import "ARLineChartContentView.h"
#import "ARLineChartCommon.h"


#define MARGIN_TOP 10
#define Y1_MARGIN_LEFT 20
#define Y2_MARGIN_RIGHT 20
#define X_MARGIN_BUTTOM 26 //底部x轴横线距离底边的高度
#define GRID_WIDTH 65 //刻度间距
#define TITLEINSETOFBLOCK 5 //标题与色块的距离
#define BLOCKGAP 12 //色块间距

@interface ARLineChartView ()

@property (strong, nonatomic) NSString *title1;
@property (strong, nonatomic) NSString *title2;
@property (strong, nonatomic) NSString *titleX;
@property (strong, nonatomic) NSString *desc1;
@property (strong, nonatomic) NSString *desc2;
@property (strong, nonatomic) NSArray *xLimits;

@property (strong, nonatomic) UIView *block1;
@property (strong, nonatomic) UIView *block2;
@property (strong, nonatomic) UILabel *label_title_1;
@property (strong, nonatomic) UILabel *label_title_2;
@property (assign, nonatomic) CGSize block_size_1;
@property (assign, nonatomic) CGSize block_size_2;
@property (assign, nonatomic) CGSize label_title_size_1;
@property (assign, nonatomic) CGSize label_title_size_2;

@property (strong, nonatomic) NSArray *dataSrc;
@property (strong, nonatomic) ARLineChartContentView *lineChartContentView;
@property(nonatomic, assign) BOOL isDual;
@end

@implementation ARLineChartView

- (id)initWithFrame:(CGRect)frame
         dataSource:(NSArray*)dataSource
             isDual:(BOOL)isDual
             xTitle:(NSString*)xTitle
            y1Title:(NSString*)y1Title
            y2Title:(NSString*)y2Title
              desc1:(NSString*)desc1
              desc2:(NSString*)desc2
            xLimits:(NSArray *)limits
{
    self = [super initWithFrame:frame];
    if (self) {
        self.isDual = isDual;
        // Initialization code
        self.backgroundColor = [UIColor clearColor];
        //        self.backgroundColor = [UIColor brownColor];
        //        self.backgroundColor = [UIColor whiteColor];
        
        //////////// test ///////////////////
        self.titleX = xTitle ? xTitle : @"X";
        self.title1 = y1Title ? y1Title : @"Y1";
        self.title2 = y2Title ? y2Title : @"Y2";
        
        self.desc1 = desc1 ? desc1 : @"Desc1";
        self.desc2 = desc2 ? desc2 : @"Desc2";
        
        self.xLimits = limits;
        //////////// end //////////////////
        
        ////////////////////// 折线图内容区域 ///////////////////////////
        self.dataSrc = dataSource;
        CGRect rect = CGRectMake(0,
                                 MARGIN_TOP,
                                 self.frame.size.width,
                                 self.frame.size.height - MARGIN_TOP - X_MARGIN_BUTTOM);
        int x_point_num = floor(self.frame.size.width/GRID_WIDTH);
        self.lineChartContentView = [[ARLineChartContentView alloc] initWithFrame:rect
                                                                       dataSource:self.dataSrc
                                                                           isDual:self.isDual
                                                                     forXpointNum:x_point_num
                                                                          xLimits:self.xLimits];
        [self addSubview:self.lineChartContentView];
        
        ////////////////////// 底部颜色示例 //////////////////////
        UIFont *font = [UIFont systemFontOfSize:11];//字体
        CGFloat block_width = 16;    CGFloat block_height = 10;
        CGSize title1size = [y1Title sizeWithAttributes:@{NSFontAttributeName:font}];//标题1size
        CGSize title2size = CGSizeMake(0, 0);//标题2size
        CGFloat xStart = 0;
        if (isDual) {
            title2size = [y2Title sizeWithAttributes:@{NSFontAttributeName:font}];
            xStart = (self.frame.size.width - block_width*2 - TITLEINSETOFBLOCK*2 - title1size.width - title2size.width - BLOCKGAP)/2;
        }
        else {
            xStart = (self.frame.size.width -  block_width - title1size.width - TITLEINSETOFBLOCK)/2;
        }
        CGRect block1 = CGRectMake(xStart, rect.origin.y + rect.size.height + 10, block_width, block_height);
        UIView *blockView1 = [[UIView alloc]initWithFrame:block1];
        self.block1 = blockView1;
        blockView1.backgroundColor = line1Color;
        UILabel *title1label = [[UILabel alloc]initWithFrame:CGRectMake(block1.origin.x + block1.size.width + TITLEINSETOFBLOCK,
                                                                   block1.origin.y-3,
                                                                   title1size.width,
                                                                   title1size.height)];
        self.label_title_1 = title1label;
        title1label.textColor = line1Color;
        title1label.font = font;
        title1label.text = y1Title;
        [self addSubview:blockView1];
        [self addSubview:title1label];
        
        if (isDual) {
            CGRect block2 = CGRectMake(xStart + block1.size.width + TITLEINSETOFBLOCK + title1size.width + BLOCKGAP, block1.origin.y, block_width, block_height);
            UIView *blockView2 = [[UIView alloc]initWithFrame:block2];
            self.block2 = blockView2;
            blockView2.backgroundColor = line2Color;
            UILabel *title2label = [[UILabel alloc]initWithFrame:CGRectMake(block2.origin.x + block2.size.width + TITLEINSETOFBLOCK,
                                                                            block2.origin.y-3,
                                                                            title2size.width,
                                                                            title2size.height)];
            self.label_title_2 = title2label;
            title2label.textColor = line2Color;
            title2label.font = font;
            title2label.text = y2Title;
            [self addSubview:blockView2];
            [self addSubview:title2label];
        }
        
        
        ////////////////////// 注册手势监听 ///////////////////////////
        UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]
                                                     initWithTarget:self
                                                     action:@selector(pinchDetected:)];
        [self addGestureRecognizer:pinchRecognizer];
        
    }
    return self;
}

- (void)refreshData:(NSArray *)dataSource{
    [self.lineChartContentView refreshData:dataSource];
}

//x轴和y轴同时放大一个刻度
- (void)zoomUp
{
    [self.lineChartContentView zoomUp];
}
//x轴和y轴同时减小一个刻度
- (void)zoomDown
{
    [self.lineChartContentView zoomDown];
}
//x轴和y轴还原到原始刻度
- (void)zoomOriginal
{
    [self.lineChartContentView zoomOriginal];
}

//x轴放大一个刻度
- (void)zoomHorizontalUp
{
    [self.lineChartContentView zoomHorizontalUp];
}
//x轴减小一个刻度
- (void)zoomHorizontalDown
{
    [self.lineChartContentView zoomHorizontalDown];
}

//y轴放大一个刻度
- (void)zoomVerticalUp
{
    [self.lineChartContentView zoomVerticalUp];
}
//y轴减小一个刻度
- (void)zoomVerticalDown
{
    [self.lineChartContentView zoomVerticalDown];
}


- (IBAction)pinchDetected:(UIPinchGestureRecognizer *)sender {
    
    //比例（经常用到放缩比例）这个属性默认值是1，通过获取放缩比例属性
    CGFloat scale =  [(UIPinchGestureRecognizer *)sender scale];
    if([sender state] == UIGestureRecognizerStateChanged) {
        if (scale >= 1.1) { //放大
            
            [self.lineChartContentView zoomUp];
        }
        else { //缩小
            [self.lineChartContentView zoomDown];
        }
    }
    
}

- (void)relayout:(CGRect)frame{
    [super setFrame:frame];
    [self.lineChartContentView removeFromSuperview];
    CGRect rect = CGRectMake(0,
                             MARGIN_TOP,
                             frame.size.width,
                             frame.size.height - MARGIN_TOP - X_MARGIN_BUTTOM);
    int x_point_num = floor(frame.size.width/GRID_WIDTH);
    self.lineChartContentView = [[ARLineChartContentView alloc] initWithFrame:rect
                                                                   dataSource:self.dataSrc
                                                                       isDual:self.isDual
                                                                 forXpointNum:x_point_num xLimits:self.xLimits];
    [self addSubview:self.lineChartContentView];
    
    CGFloat xStart = 0;
    if (self.isDual) {
        xStart = (self.frame.size.width - self.block1.frame.size.width*2 - TITLEINSETOFBLOCK*2 - self.label_title_1.frame.size.width - self.label_title_2.frame.size.width - BLOCKGAP)/2;
    }
    else {
        xStart = (self.frame.size.width -  self.block1.frame.size.width - self.label_title_1.frame.size.width - TITLEINSETOFBLOCK)/2;
    }
    CGRect tframe = self.block1.frame;
    tframe.origin.x = xStart;
    [self.block1 setFrame:tframe];
    tframe = self.label_title_1.frame;
    tframe.origin.x = self.block1.frame.origin.x + self.block1.frame.size.width + TITLEINSETOFBLOCK;
    [self.label_title_1 setFrame:tframe];
    if (self.isDual) {
        tframe = self.block2.frame;
        tframe.origin.x = xStart + self.block1.frame.size.width + TITLEINSETOFBLOCK + self.label_title_1.frame.size.width + BLOCKGAP;
        [self.block2 setFrame:tframe];
        tframe = self.label_title_2.frame;
        tframe.origin.x = self.block2.frame.origin.x + self.block2.frame.size.width + TITLEINSETOFBLOCK;
        [self.label_title_2 setFrame:tframe];
    }
}

@end




